var gulp = require('gulp');
var connect = require('gulp-connect');

// 跨域设置
var cors = function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', '*');
    res.setHeader('Access-Control-Allow-Methods', '*');
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    // 浏览器缓存预检请求结果时间,单位:秒，24小时
    res.setHeader('Access-Control-Max-Age', '86400');
    next();
};
// http server
gulp.task('connect_dist', function () {
    connect.server({
        root: './dist',
        host: "0.0.0.0",
        port: 8900,
        livereload: true,    // 自动刷新
        middleware: function (connect, opt) {
            return [cors];
        }
    });
});

//定义html任务
gulp.task('html', function () {
    gulp.src("./dist")
        .pipe(connect.reload());
});
gulp.task('watch', function () {
    gulp.watch('dist/**/*.*', ['html']);
})

gulp.task('connect', gulp.series('connect_dist', 'watch', function () {
    // default task code here
}));

